package com.troila.cloud.simplify.web.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.troila.cloud.simplify.model.STitle;
import com.troila.cloud.simplify.service.STitleService;

@Controller
@RequestMapping("/")
public class WelcomeController {
	@Autowired
	private STitleService sTitleService;
	
	@GetMapping("/back/list")
	public String index(HttpSession session,Model model,HttpServletRequest request, HttpServletResponse response) {
		String mediaInfo = request.getAttribute("mediaInfo").toString();
		JSONObject  miJson = JSON.parseObject(mediaInfo);
		session.setAttribute("mediaId", miJson.getString("media_id"));
//		session.setAttribute("mediaId", "gh_43d7956970ca");
		return "/back/list";
	}
	
	@GetMapping("/back/product")
	public String product(HttpSession session,Model model,@RequestParam String titleId) {
		session.setAttribute("titleId", titleId);
		STitle st = sTitleService.findById(Integer.valueOf(titleId));
		if(st != null) {
			session.setAttribute("title", st.gettName());
		}else {
			session.setAttribute("title", "魔镜魔镜 ");
		}
		
		return "/back/productAdd";
	}
	
	public static void main(String[] args) throws IOException {
		Document doc = Jsoup.connect("http://m.8qizi.com/").userAgent("Mozilla").get();

		Element link = doc.select("a").first();
		String relHref = link.attr("href"); // == "/"
		String absHref = link.attr("abs:href"); // "http://www.open-open.com/"
		System.out.println(absHref);
		System.out.println(relHref);
//		System.out.println(doc.title());
		System.out.println(doc.html());
		
		Elements links = doc.select("a[href]"); //带有href属性的a元素
	    for (Element linkinput : links) {
	    	System.out.println(linkinput.tagName());
	    	System.out.println(linkinput.attr("abs:href"));
	    	System.out.println(linkinput.attr("rel"));
        }
	}

}
